{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fill cell metadata for TCGA samples\n",
    "\n",
    "Create a table containing the following:\n",
    "\n",
    "- Deconvolution result => ITTH score [DONE]\n",
    "- NMF results\n",
    "- Drug response\n",
    "- Survival "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.stats import entropy "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "GDSC_decon_df = pd.read_csv('../data/CIBERSORT_related/GDSC_subtype_panel/unc_gdsc_output_full.csv', index_col=0)\n",
    "LM_decon_df = pd.read_csv('../data/CIBERSORT_related/GDSC_subtype_panel/unc_gdsc_output_full.csv', index_col=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If percentage < 0.05, then set to 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "GDSC_decon_df = GDSC_decon_df.where(GDSC_decon_df >= 0.05, other=0)\n",
    "LM_decon_df = LM_decon_df.where(LM_decon_df >= 0.05, other=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove P-value, correlation, and RMSE columns\n",
    "GDSC_decon_df = GDSC_decon_df[GDSC_decon_df.columns[0:-3]]\n",
    "LM_decon_df = LM_decon_df[GDSC_decon_df.columns[0:-3]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get patient id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "GDSC_decon_df.head()\n",
    "\n",
    "patient_list = ['-'.join(s.split('-')[0:3]) for s in GDSC_decon_df.index]\n",
    "acc_list = [s.split('-')[1] for s in GDSC_decon_df.index]\n",
    "\n",
    "meta_df = pd.DataFrame(patient_list, index=GDSC_decon_df.index, columns=['patient_id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "tss_to_acr_dict = pd.read_pickle('../preprocessed_data/TCGA/tss_to_acronym_dict.pkl')\n",
    "meta_df.loc[:, 'cancer_type'] = [tss_to_acr_dict[acc] for acc in acc_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate entropy (ITTH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "meta_df.loc[:, 'ITTH'] = entropy(pk=GDSC_decon_df.T, qk=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient_id</th>\n",
       "      <th>cancer_type</th>\n",
       "      <th>ITTH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Input Sample</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>TCGA-OR-A5J1-01A-11R-A29S-07</th>\n",
       "      <td>TCGA-OR-A5J1</td>\n",
       "      <td>ACC</td>\n",
       "      <td>2.174661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TCGA-OR-A5J2-01A-11R-A29S-07</th>\n",
       "      <td>TCGA-OR-A5J2</td>\n",
       "      <td>ACC</td>\n",
       "      <td>1.734458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TCGA-OR-A5J3-01A-11R-A29S-07</th>\n",
       "      <td>TCGA-OR-A5J3</td>\n",
       "      <td>ACC</td>\n",
       "      <td>1.896308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TCGA-OR-A5J5-01A-11R-A29S-07</th>\n",
       "      <td>TCGA-OR-A5J5</td>\n",
       "      <td>ACC</td>\n",
       "      <td>1.905145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TCGA-OR-A5J6-01A-31R-A29S-07</th>\n",
       "      <td>TCGA-OR-A5J6</td>\n",
       "      <td>ACC</td>\n",
       "      <td>2.045562</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                patient_id cancer_type      ITTH\n",
       "Input Sample                                                    \n",
       "TCGA-OR-A5J1-01A-11R-A29S-07  TCGA-OR-A5J1         ACC  2.174661\n",
       "TCGA-OR-A5J2-01A-11R-A29S-07  TCGA-OR-A5J2         ACC  1.734458\n",
       "TCGA-OR-A5J3-01A-11R-A29S-07  TCGA-OR-A5J3         ACC  1.896308\n",
       "TCGA-OR-A5J5-01A-11R-A29S-07  TCGA-OR-A5J5         ACC  1.905145\n",
       "TCGA-OR-A5J6-01A-31R-A29S-07  TCGA-OR-A5J6         ACC  2.045562"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "meta_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
